package com.andnnl.jetty;

import com.andnnl.config.ConfigFileHelper;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by chenss on 2023/8/24.
 */
public class MoHttpServer extends AbstractHandler {
    private static Logger log = LoggerFactory.getLogger(MoHttpServer.class);

    @Override
    public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        response.setStatus(HttpServletResponse.SC_OK);
        baseRequest.setHandled(true);
        String uri = request.getRequestURI();
        log.info("target:"+target);
        log.info("uri:"+uri);

        String rs = "100";
        try {
//			request.setCharacterEncoding(encode);
            String encode="UTF-8";
            response.setContentType("text/html;charset=" + encode);
            BufferedReader streamReader = new BufferedReader(new InputStreamReader(request.getInputStream(), encode));
            StringBuilder sb = new StringBuilder();
            String inputStr;
            while ((inputStr = streamReader.readLine()) != null)
                sb.append(inputStr);
            log.info("body:" + sb.toString());

        } catch (Exception e) {
            rs = e.getMessage();
            log.error(rs, e);
        }
        response.getWriter().println(rs);
    }
}
